package com.farm.learn.service.impl;

import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.time.TimeTool;
import com.farm.learn.dao.ClassUserDaoInter;
import com.farm.learn.domain.ClassUser;
import com.farm.learn.service.ClassUserServiceInter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClassUserServiceImpl implements ClassUserServiceInter {
    @Resource
    private ClassUserDaoInter classUserDaoImpl;

    @SuppressWarnings("unused")
    private static final Logger log = Logger.getLogger(ClassUserServiceImpl.class);

    @Override
    @Transactional
    public ClassUser insertClassUserEntity(ClassUser entity, LoginUser user) {
        entity.setCuser(user.getId());
        return classUserDaoImpl.insertEntity(entity);
    }

    @Override
    @Transactional
    public ClassUser editClassUserEntity(ClassUser entity, LoginUser user) {

        ClassUser entity2 = classUserDaoImpl.getEntity(entity.getId());
        entity2.setClassid(entity.getClassid());
        entity2.setCuser(entity.getCuser());
        entity2.setId(entity.getId());
        classUserDaoImpl.editEntity(entity2);
        return entity2;
    }

    @Override
    @Transactional
    public void deleteClassUserEntity(String id, LoginUser user) {

        classUserDaoImpl.deleteEntity(classUserDaoImpl.getEntity(id));
    }

    @Override
    @Transactional
    public ClassUser getClassUserEntity(String id) {

        if (id == null) {
            return null;
        }
        return classUserDaoImpl.getEntity(id);
    }

    @Override
    @Transactional
    public DataQuery createClassUserSimpleQuery(DataQuery query) {

        DataQuery dbQuery = DataQuery
            .init(
                query,
                "WLP_L_USER",
                "ID,CLASSID,CUSER");
        return dbQuery;
    }

    @Override
    @Transactional
    public ClassUser findLearnByClassId(String classId, String userid) {
        List<ClassUser> list = classUserDaoImpl.findLearnByClassId(classId, userid);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public ClassUserDaoInter getClassUserDaoImpl() {
        return classUserDaoImpl;
    }

    public void setClassUserDaoImpl(ClassUserDaoInter dao) {
        this.classUserDaoImpl = dao;
    }

}
